package com.study.d016event;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
*@description 通过addEventFilter() 和addEventHandler() 方法添加事件的区别：
*addEventFilter() : 事件由最外层的父容器先子组件传递
*addEventHandler(): 事件由子组件向最外层的父容器传递
*@author xtayhame
*date 2020年3月8日
*version
*since v1.0
*/
public class FilterAndHandlerEventMain extends Application{
	public static void main(String[] args) {
		launch(args);
	}
	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane = new AnchorPane();
		HBox hBox = new HBox(20);
		Button b1 = new Button("按钮1");
		Label label = new Label("标签");
		label.setStyle("-fx-background-color:#ff9876");
		label.setPrefSize(100, 50);

		hBox.getChildren().addAll(b1,label);
		anchorPane.getChildren().add(hBox);
		Scene scene = new Scene(anchorPane, 400, 500);
		stage.setScene(scene);
		stage.show();

		/*System.out.println("**********************addEventFilter事件**********************");
		b1.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				System.out.println("【b1】.addEventFilter--->Source="+event.getSource() + "  Target=" + event.getTarget());
			}
		});

		hBox.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				System.out.println("【hBox】.addEventFilter--->Source="+event.getSource() + "  Target=" + event.getTarget());
			}
		});
		anchorPane.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				System.out.println("【anchorPane】.addEventFilter--->Source="+event.getSource() + "  Target=" + event.getTarget());
				event.consume();
			}
		});

		*/

		b1.setOnMouseClicked(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent event) {
				System.out.println("b1按钮被点击了.");
			}
		});
		hBox.setOnMouseClicked(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent event) {
				System.out.println("hBox被点击了.");
			}
		});




		System.out.println("**********************addEventHandler事件**********************");
		label.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				System.out.println("【label】.addEventHandler--->Source="+event.getSource() + "  Target=" + event.getTarget());
				event.consume();
			}
		});
		hBox.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				System.out.println("【hBox】.addEventHandler--->Source="+event.getSource() + "  Target=" + event.getTarget());
			}
		});
		anchorPane.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				System.out.println("【anchorPane】.addEventHandler--->Source="+event.getSource() + "  Target=" + event.getTarget());
			}
		});

	}
}
